﻿using System;
using System.Collections.Specialized;
using FileMonitor.Service.Prototype;

namespace FileMonitor.Service.Provider.ProcessProvider
{
    public class ProcessProvider
    {
        private static FileMonitorProcessProviderBase p;
        private static object lockObj = new object();
        public static FileProcessProviderCollection GetAllProcess()
        {
            FileMonitorProcessProviderBase instance = GetInstance();
            return instance.GetAllProcess();
        }

        public static void LockProcess(FileMonitorBase process)
        {
            FileMonitorProcessProviderBase instance = GetInstance();
            instance.LockProcess(process);
        }

        public static FileMonitorProcessProviderBase GetInstance()
        {
            if (p == null)
            {
                lock (lockObj)
                {
                    if (p == null)
                    {
                        string processProvierType = System.Configuration.ConfigurationManager.AppSettings["ConfigProviderType"];
                        if (string.IsNullOrEmpty(processProvierType))
                        {
                            processProvierType = "AppConifg";
                        }
                        NameValueCollection config = new System.Collections.Specialized.NameValueCollection();
                        if (processProvierType == "DataBase")
                        {
                            string providerDbConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultExchangeDB"].ConnectionString;
                            config["ProvderDBConnection"] = providerDbConnStr;
                        }
                        if (processProvierType == "XMLConfig")
                        {
                            string providerDbConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultExchangeDB"].ConnectionString;
                            config["ProvderDBConnection"] = providerDbConnStr;
                        }

                        string typeName = string.Format("FileMonitor.Service.Provider.ProcessProvider.FileMonitorProcess{0}Provider", processProvierType);
                        Type type = Type.GetType(typeName, true, true);
                        Object base1 = Activator.CreateInstance(type);
                        p = base1 as FileMonitorProcessProviderBase;
                        if (p != null)
                        {
                            p.Initialize(config);

                        }
                    }
                }
            }
            return p;
        }
    }
}
